using System;
using System.Collections.Generic;
using UnityEngine;
public class ScaleGUI
{
    public static bool scaleScreen;
    public static float WIDTH, HEIGHT;
    static List<Matrix4x4> stack = new List<Matrix4x4>();
    public static int numScale = 1;
   // public static Map map = new Map();
    static public void initScaleGUI()
    {
        Debug.Log("Init Scale GUI: Screen.w="+Screen.width+" Screen.h=" +Screen.height);
        WIDTH = Screen.width;
        HEIGHT = Screen.height;

        scaleScreen = false;
        if (Screen.width > 1200)// IPAD 2
        {
            //scaleScreen = true;
            //WIDTH = Screen.width / 3;
            //HEIGHT = Screen.height / 3;
            numScale = 1;
            //numScale = map.GetHeight() / Screen.height;
        }
        else if (Screen.height > 480)
            //numScale = map.GetHeight() / Screen.height;
            numScale = 1;

        //		if(Screen.width==768)// IPAD
        //		{
        //			scaleScreen=true;
        //			WIDTH=Screen.width/2;
        //			HEIGHT=Screen.height/2;
        //		}
        //		if(Screen.width==640) // IPHONE 4
        //		{
        //			scaleScreen=true;
        //			WIDTH=Screen.width/2;
        //			HEIGHT=Screen.height/2;
        //		}
        //WIDTH=320;
        //HEIGHT=480;
        //scaleScreen=true;
        else if (Screen.height <= 480)
        {
            //numScale = 1080/ Screen.height;
            numScale = 2;
        }
    }
    static public void BeginGUI()
    {
        if (!scaleScreen) return;
        stack.Add(GUI.matrix);
        Matrix4x4 m = new Matrix4x4();
        var w = (float)Screen.width;
        var h = (float)Screen.height;
        var aspect = w / h;
        var scale = 1f;
        var offset = Vector3.zero;
        if (aspect < (WIDTH / HEIGHT))
        { //screen is taller
            scale = (Screen.width / WIDTH);

        }
        else
        { // screen is wider
            scale = (Screen.height / HEIGHT);
        }
        m.SetTRS(offset, Quaternion.identity, Vector3.one * scale);
        GUI.matrix *= m;
    }

    static public void EndGUI()
    {
        if (!scaleScreen) return;
        GUI.matrix = stack[stack.Count - 1];
        stack.RemoveAt(stack.Count - 1);
    }

    public static float scaleX(float x)
    {
        if (!scaleScreen) return x;
        x = (x * WIDTH) / Screen.width;
        return x;
    }
    public static float scaleY(float y)
    {
        if (!scaleScreen) return y;
        y = (y * HEIGHT) / Screen.height;
        return y;
    }
}


